/*
 * graph2D
 * Copyright (c) 2012 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "variableContainer.h"
#include "abort.h"

namespace Graph2D
{
	static const int DEFAULT_PAGE_SIZE = 10;

	VariableContainer::VariableContainer()
		: children(NULL)
		, allocatedSize(0)
		, pageSize(DEFAULT_PAGE_SIZE)
    {
		setType(TYPE_VARIABLE_CONTAINER);
	}

	VariableContainer::~VariableContainer()
	{
		clear();
	}

	void VariableContainer::allocate(const size_t count)
	{
		for(size_t i = 0; i < allocatedSize; i++)
		{
//			if(children[i])
//				children[i]->release();
		}

		children = (Component**)realloc(children, count * sizeof(Component*));
		if(children == NULL)
			abort(ERROR_CODE_NOT_ENOUGH_MEMORY);

		memset(children, 0, count * sizeof(Component*));

		allocatedSize = count;
	}

	size_t VariableContainer::getCount() const
	{
		return allocatedSize;
	}

	Component* VariableContainer::get(const size_t index) const
	{
		return (index < allocatedSize) ? children[index] : NULL;
	}

	void VariableContainer::set(const size_t index, Component* component)
	{
		if(index >= allocatedSize)
		{
			children = (Component**)realloc(children, (index + pageSize) * sizeof(Component*));
			if(children == NULL)
				abort(ERROR_CODE_NOT_ENOUGH_MEMORY);

			memset(&children[allocatedSize], 0, (index + pageSize - allocatedSize) * sizeof(Component*));
			allocatedSize = index + pageSize;
		}

		if(children[index])
		{
			//children[index]->release();
			children[index]->setParent(NULL);
		}

		children[index] = component;
		if(children[index])
		{
			//children[index]->retain();
			children[index]->setParent(this);
		}
	}

	void VariableContainer::append(Component* component)
	{
#if 0
		for(long i = (long)allocatedSize - 1; i >= 0; i--)
#else
		for(size_t i = 0; i < allocatedSize; i++)
#endif
		{
			Component* child = children[i];
			if(child == NULL)
			{
				set(i, component);
				return;
			}
		}

		set(allocatedSize, component);
	}

	void VariableContainer::append(Component& component)
	{
		append(&component);
	}

	void VariableContainer::swap(const size_t to, const size_t from)
	{
		Component* component = get(to);
		/*
		if(component)
			component->retain();
			*/
		set(to, get(from));
		set(from, component);
		/*
		if(component)
			component->release();
			*/
	}

	void VariableContainer::clear()
	{
		super::clear();

		free(children);
		children = NULL;
		allocatedSize = 0;
	}

	void VariableContainer::compact()
	{
		long index = (long)allocatedSize - 1;
		while(index > 0)
		{
			if(children[index])
				break;
			--index;
		}
		if(index < 0)
		{
			clear();
		}
		else if(allocatedSize < static_cast<size_t>(index + 1))
		{
			allocatedSize = index + 1;
			children = (Component**)realloc(children, allocatedSize * sizeof(Component*));
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	bool VariableContainer::compare(const VariableContainer& other) const
	{
		if(!super::compare(other))
			return false;

		if(allocatedSize != other.allocatedSize)
			return false;

		for(size_t i = 0; i < allocatedSize; i++)
		{
			if(children[i] == NULL && other.children[i] == NULL)
				continue;
			if(children[i] == NULL || other.children[i] == NULL)
				return false;

			if(children[i]->compare(*other.children[i]) == false)
				return false;
		}
#if 0
		if(flags != other.flags)
			return false;
		if(toucheBeganScrollOffset != other.toucheBeganScrollOffset)
			return false;
		if(scrollOffset != other.scrollOffset)
			return false;
		if(scrollTargetOffset != other.scrollTargetOffset)
			return false;
		if(scrollOffsetLimit != other.scrollOffsetLimit)
			return false;
#endif
		return true;
	}
}
